package com.morn.fanfou.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.morn.fanfou.ConversationActivity;
import com.morn.fanfou.FanfouApplication;
import com.morn.fanfou.R;
import com.morn.fanfou.api.Fanfou;
import com.morn.fanfou.model.Conversation;
import com.morn.fanfou.widget.ConversationListAdapter;

public class MessageFragment extends Fragment implements OnItemClickListener {
	private ListView listView;
	private Fanfou fanfou;
	private BaseAdapter adapter;
	private List<Conversation> conversationList = new ArrayList<Conversation>();

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		fanfou = ((FanfouApplication) activity.getApplication()).getFanfou();

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.list_layout, container, false);
		TextView titleText = (TextView) v.findViewById(R.id.header_title_text);
		titleText.setText(R.string.header_title_message);

		ImageView leftImage = (ImageView) v.findViewById(R.id.header_image_right);
		leftImage.setVisibility(View.GONE);

		ImageView rightImage = (ImageView) v.findViewById(R.id.header_image_left);
		rightImage.setImageResource(R.drawable.ic_menu_edit);
		listView = (ListView) v.findViewById(R.id.mListView);
		listView.setCacheColorHint(Color.TRANSPARENT);
		adapter = new ConversationListAdapter(this.getActivity(), conversationList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		new GetStatusTask().execute();
	}

	private class GetStatusTask extends AsyncTask<Object, Object, String> {

		private List<com.morn.fanfou.model.Conversation> list = null;

		@Override
		protected String doInBackground(Object... params) {
			String error = "OK";
			try {
				list = fanfou.getConversationList(null, null);
			} catch (Exception e) {
				error = e.getMessage();
				e.printStackTrace();
			}
			return error;
		}

		@Override
		protected void onPostExecute(String result) {
			if ("OK".equals(result)) {
				if (list != null) {
					conversationList.clear();
					conversationList.addAll(list);
					adapter.notifyDataSetChanged();
				} else {
					Toast.makeText(MessageFragment.this.getActivity(), "service not response", Toast.LENGTH_SHORT)
							.show();
				}
			} else {
				Toast.makeText(MessageFragment.this.getActivity(), result, Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Conversation conversation = conversationList.get(position);
		Intent intent = new Intent(this.getActivity(), ConversationActivity.class);
		intent.putExtra("otherId", conversation.getOtherid());
		this.startActivity(intent);
	}

}
